#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
void writer(int wfd)
{
    const char*str="Hello father process,I am a child process";
    pid_t pid=getpid();
    int cnt = 0;
    char buffer[128];
    while(1)
    {
        snprintf(buffer,sizeof(buffer),"message:%s,pid:%d,count:%d\n",str,pid,cnt);
        write(wfd,buffer,sizeof(buffer));
        cnt++;
        sleep(1);
    }
}
void reader(int rfd)
{
    char buff[1024];
    while(1)
    {
        ssize_t n=read(rfd,buff,sizeof(buff)-1);
        (void)n;
        printf("%s",buff);
    }
}
int main()
{
    int pipefd[2];
    int n=pipe(pipefd);
    if(n<0)
    {
        return 0;
    }
    /*
     *pipefd[0]:r方式
     *pipefd[1]:w方式
     * */
    printf("pipe[0]:%d,pipe[1]:%d\n",pipefd[0],pipefd[1]);
    pid_t id=fork();
    if(id==0)
    {
        //child process
        //w
        close(pipefd[0]);
        writer(pipefd[1]);
        exit(0);
    }else{
        //father process
        //r
        close(pipefd[1]);
        reader(pipefd[0]);
        wait(NULL);
    }
    return 0;
}
